package cn.neusoft.service.impl;


import cn.neusoft.mapper.FoodMapper;
import cn.neusoft.pojo.Food;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.IFoodService;
import cn.neusoft.vo.ResultBean;
import cn.neusoft.vo.PageBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 这里调用dao层的封装
 * servlet调用服务层
 */
@Service
public class FoodService implements IFoodService {
	@Autowired
	private FoodMapper foodMapper;


	@Override
	@Transactional
	public ResultBean add(Food food) throws Exception {
		//参数检验
		if(StringUtils.isEmpty(food.getFoodName())||food==null) {
			return ResultBean.ERROR("params is null");
		}
		int count=foodMapper.add(food);
		return count>0?ResultBean.SUCCESS():ResultBean.ERROR("add food is fail");
	}

	@Override
	@Transactional
	public ResultBean update(Food food) throws Exception {
		if(StringUtils.isEmpty(food.getFoodName())||food==null
		||food.getFoodId()==null) {
			return ResultBean.ERROR("params is null");
		}
		int count=foodMapper.update(food);
		return count>0?ResultBean.SUCCESS():ResultBean.ERROR("update food  is fail");
	}

	@Override
	@Transactional
	public ResultBean delete(Integer id) throws Exception {
		if(id==null) {
			return ResultBean.ERROR("params is null");
		}
		int count=foodMapper.delete(id);
		return count>0?ResultBean.SUCCESS():ResultBean.ERROR("delete food is fail");
	}

	@Override
	@Transactional(readOnly = true)
	public ResultBean getById(Integer id) throws Exception {
		if(id==null) {
			return ResultBean.ERROR("params is null");
		}
		Food food=foodMapper.getById(id);
		return food==null?ResultBean.ERROR("get food by is fail"):
				ResultBean.SUCCESS(food);
	}

	@Override
	@Transactional(readOnly = true)
	public ResultBean getAll() throws Exception {
		List<Food> foods = foodMapper.getAll();
		return ResultBean.SUCCESS(foods);
	}

	@Override
	@Transactional(readOnly = true)
	public ResultBean getByPage(BaseQuery baseQuery) throws Exception {
		PageBean<Food> pageBean=new PageBean<>();
		int count=foodMapper.getCount(baseQuery);
		pageBean.setTotalRows(count);
		if(count%baseQuery.getPageCount()==0) {
			pageBean.setTotalPages(count/baseQuery.getPageCount());
		} else {
			pageBean.setTotalPages(count/baseQuery.getPageCount()+1);
		}
		//通过分页得到结果
		//再将结果进行打印
		List<Food> foodList=foodMapper.getByPage(baseQuery);
		//foodList.forEach(System.out::println);
		//将分页的结果，给pageBean，然后再将这个对象给ResultBean封装起来
		//再将这个ResultBean返回给前端，前端可以在vue中进行查看.
		pageBean.setData(foodList);
		return ResultBean.SUCCESS(pageBean);
	}
	@Override
	@Transactional
	public ResultBean batchDel(List<Integer> ids) throws Exception {
		if(ids==null|| ids.size()==0) {
			return ResultBean.ERROR("params is null");
		}
		for(Integer id:ids) {
			int delete = foodMapper.delete(id);
			if(delete<1) {
				return ResultBean.ERROR("delete is failed");
			}
		}
		return ResultBean.SUCCESS();
	}
}
